package com.springexample.demo.exception;

import com.springexample.demo.component.ExceptionEnum;
import com.springexample.demo.component.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 1、全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler<T> {

    @ExceptionHandler({Exception.class})
    public Response IllegalStateExceptionHandler(Exception exception){
        log.error("捕获到异常：",exception);
        ExceptionEnum e = ExceptionEnum.getByException(exception.getClass());
        return Response.builder().code(e.getCode()).flag(false).data(e.getMessage()).build();
    }

}
